/*
 * Dijjer - A Peer to Peer HTTP Cache
 * Copyright (C) 2004,2005 Change.Tv, Inc
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package dijjer.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import dijjer.util.logging.Logger;


public class ArgParser {

	public static final String VERSION = "$Id: ArgParser.java 2 2008-03-21 14:33:03Z ian.clarke $";
	private final String _name;
	private final boolean _allowUnknown;

	private final Map _descriptions = new HashMap();
	private final Map _actions = new HashMap();
	private final List _unmatched = new ArrayList();
	private final List _flags = new ArrayList();
	private final List _ignore = new ArrayList();

	public ArgParser(String name, boolean allowUnknown) {
		_name = name;
		_allowUnknown = allowUnknown;
	}

	public void addArgument(String name, String description, ArgumentAction action) {
		name = name.toLowerCase();
		_descriptions.put(name, description);
		_actions.put(name, action);
	}

	public void addUnmatchedKeyArgument(String name, String description, ArgumentAction action) {
		name = name.toLowerCase();
		_descriptions.put(name, description);
		_actions.put(name, action);
		_unmatched.add(name);
	}

	public void ignoreArgument(String arg) {
		_ignore.add(arg.toLowerCase());
	}

	public void parse(String[] args) throws Exception {
		for (int i = 0; i < args.length; i++) {
			String arg = args[i];
			String name = arg;
			String value = null;
			if (arg.startsWith("--")) {
				_flags.add(arg);
				continue;
			}

			if (!_unmatched.contains(arg.toLowerCase())) {
				if (_ignore.contains(arg.toLowerCase())) {
					continue;
				}
				int eP = arg.indexOf("=");
				if (eP == -1) {
					Logger.fatal(-1, "Argument \"" + arg + "\" not in key=value format");
				}
				String[] sa = new String[2];
				sa[0] = arg.substring(0, eP);
				sa[1] = arg.substring(eP+1, arg.length());

				name = sa[0].toLowerCase();
				if (_ignore.contains(name)) {
					continue;
				}
				if (!_actions.containsKey(name)) {
					if (_allowUnknown) {
						continue;
					}
					printValidArguments();
					Logger.fatal(-1, "Unrecognised argument \"" + name + "\"");
				}
				value = sa[1];
			}
			ArgumentAction action = (ArgumentAction) _actions.get(name);
			action.performAction(value);
		}
	}

	protected void printValidArguments() {
		Logger.out(_name + " <argument>=<value> <argument>=<value> ...");
		Logger.out("");
		Logger.out("Valid arguments: ");
		for (Iterator i = _descriptions.keySet().iterator(); i.hasNext();) {
			String name = (String) i.next();
			Logger.out(name + "\t" + _descriptions.get(name));
		}
		Logger.out("");
	}

	public static interface ArgumentAction {
		public void performAction(String arg) throws Exception;
	}

}
